package cn.bdqn.pn.controller.commodity;

import cn.bdqn.pn.pojo.commodity.Showtype;
import cn.bdqn.pn.service.commodity.ShowTypeService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

@Controller
@RequestMapping(value = "/showtype")
public class ShowTypeController {
    @Autowired
    private ShowTypeService showTypeService;

    /**
     * 绑定并进行分页
     * @param modelMap
     * @param pageNo
     * @param pageSize
     * @param fatherId
     * @return
     * @throws Exception
     */
    @RequestMapping("/listType") //进页面，
    public String listType(ModelMap modelMap,
                           @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "fatherId",defaultValue = "0") Integer fatherId) throws Exception {
        Showtype showtype=new Showtype();
        showtype.setFatherId(fatherId);
        Page<Showtype> right = showTypeService.findshowTypeNoCriteria(pageNo, pageSize,showtype);
        List<Showtype> left=showTypeService.listShowTypeByFatherId(new Showtype(fatherId));
        modelMap.addAttribute("right", right);
        modelMap.addAttribute("left",left);

        System.out.println("----------left:"+JSON.toJSONString(right));
        System.out.println("----------right:"+JSON.toJSONString(right));
        return "typeManage";
    }

    /**
     * 增加
     * @param showtype
     * @return
     * @throws Exception
     */
    @RequestMapping("/add")
    public ModelAndView add(Showtype showtype) throws Exception {
        showTypeService.addshowType(showtype);
        List<Showtype> showtypes = showTypeService.listShowTypeByFatherId(new Showtype(0));
        ModelAndView modelAndView = new ModelAndView();
        Page<Showtype> datas = showTypeService.findshowTypeNoCriteria(0, 4);
        modelAndView.addObject("right",datas);
        modelAndView.addObject("left", showtypes);
        modelAndView.setViewName("typeManage");
        return modelAndView;
     }

    /**
     * 删除
     * @param showTypeId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer showTypeId) {
        try {
            showTypeService.delete(showTypeId);
        /*return "redirect:listType";*/
       return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 查询类别父ID
     * @param fatherId
     * @return
     */
    @RequestMapping("father")
    @ResponseBody
    public List<Showtype> fatherId(Integer fatherId){
        List<Showtype> showtypeList= showTypeService.getAllfatherId(fatherId);
        return  showtypeList;
    }
    /**
     * 进首页
     * @return
     */
    @RequestMapping("showindex")
    public String showindex(){
        return "shops_index";
    }

}
